package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Sport;
import com.itheima.service.SportService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sport")
public class SportController {
    @Reference
    private SportService sportService;

    //文件上传,实现预约设置数据批量导入
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            ArrayList<Sport> data = new ArrayList<>();
            for (String[] strings : list) {
                String code = strings[0];
                String name = strings[1];
                String time = strings[2];
                String frequency = strings[3];
                String kj = strings[4];
                String o2 = strings[5];
                Sport sport = new Sport(null, code, name, time, frequency, kj, o2);
                data.add(sport);
            }
            //通过dubbo远程调用服务实现数据批量导入到数据库
            sportService.add(data);
            return new Result(true, MessageConstant.IMPORT_LIB_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            //文件解析失败
            return new Result(false, MessageConstant.IMPORT_LIB_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = sportService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Sport sport) {
        try {
            sportService.save(sport);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_SPORT_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SPORT_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            sportService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SPORT_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SPORT_SUCCESS);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Sport sport) {
        try {
            sportService.edit(sport);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_SPORT_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_SPORT_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Sport sport = sportService.findById(id);
            return new Result(true, MessageConstant.QUERY_SUCCESS, sport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_FAIL);
        }
    }
}
